#include "app_adc.h"
#include "app_main_can.h"
#include "bsp_adc.h"
#include "app_main_can.h"

const float ntc_temperature[161][2] = { 
    {336.6,-40},
    {315.0,-39},
    {295.0,-38},
    {276.4,-37},
    {259.0,-36},
    {242.8,-35},
    {227.8,-34},
    {213.8,-33},
    {200.6,-32},
    {188.4,-31},
    {177.0,-30},
    {166.4,-29},
    {156.6,-28},
    {147.2,-27},
    {138.5,-26},
    {130.4,-25},
    {122.9,-24},
    {115.8,-23},
    {109.1,-22},
    {102.9,-21},
    {97.12,-20},
    {91.66,-19},
    {86.54,-18},
    {81.72,-17},
    {77.22,-16},
    {72.98,-15},
    {69.00,-14},
    {65.26,-13},
    {61.76,-12},
    {58.46,-11},
    {55.34,-10},
    {52.42,-9},
    {49.66,-8},
    {47.08,-7},
    {44.64,-6},
    {42.34,-5},
    {40.16,-4},
    {38.12,-3},
    {36.20,-2},
    {34.38,-1},
    {32.66,0},
    {31.04,1},
    {29.50,2},
    {28.06,3},
    {26.68,4},
    {25.40,5},
    {24.18,6},
    {23.02,7},
    {21.92,8},
    {20.88,9},
    {19.90,10},
    {18.97,11},
    {18.29,12},
    {17.26,13},
    {16.46,14},
    {15.71,15},
    {15.00,16},
    {14.32,17},
    {13.68,18},
    {13.07,19},
    {12.49,20},
    {11.94,21},
    {11.42,22},
    {10.92,23},
    {10.45,24},
    {10.00,25},
    {9.574,26},
    {9.166,27},
    {8.778,28},
    {8.480,29},
    {8.058,30},
    {7.724,31},
    {7.404,32},
    {7.098,33},
    {6.808,34},
    {6.532,35},
    {6.268,36},
    {6.015,37},
    {5.776,38},
    {5.546,39},
    {5.326,40},
    {5.118,41},
    {4.918,42},
    {4.726,43},
    {4.544,	44},
    {4.368,	45},
    {4.202,	46},
    {4.042,	47},
    {3.888,	48},
    {3.742,	49},
    {3.602,	50},
    {3.468,	51},
    {3.340,	52},
    {3.216,	53},
    {3.098,	54},
    {2.986,	55},
    {2.878,	56},
    {2.774,	57},
    {2.674,	58},
    {2.580,	59},
    {2.488,	60},
    {2.400,	61},
    {2.316,	62},
    {2.234,	63},
    {2.158,	64},
    {2.082,	65},
    {2.012,	66},
    {1.942,	67},
    {1.876,	68},
    {1.813,	69},
    {1.751,	70},
    {1.693,	71},
    {1.637,	72},
    {1.582,	73},
    {1.530,	74},
    {1.480,	75},
    {1.432,	76},
    {1.385,	77},
    {1.341,	78},
    {1.298,	79},
    {1.256,	80},
    {1.210,	81},
    {1.178,	82},
    {1.141,	83},
    {1.105,	84},
    {1.071,	85},
    {1.038,	86},
    {1.006,	87},
    {0.975,	88},
    {0.945,	89},
    {0.916,	90},
    {0.888,	91},
    {0.862,	92},
    {0.836,	93},
    {0.811,	94},
    {0.787,	95},
    {0.764,	96},
    {0.741,	97},
    {0.720,	98},
    {0.699,	99},
    {0.679,	100},
    {0.659,	101},
    {0.640,	102},
    {0.622,	103},
    {0.605,	104},
    {0.588,	105},
    {0.571,	106},
    {0.555,	107},
    {0.540,	108},
    {0.525,	109},
    {0.510,	110},
    {0.496,	111},
    {0.483,	112},
    {0.470,	113},
    {0.457,	114},
    {0.445,	115},
    {0.433,	116},
    {0.421,	117},
    {0.410,	118},
    {0.399,	119},
    {0,	120},
};

static uint8_t TemplateConvert(uint16_t vol)
{
    uint8_t tmp   = 0;
    float   r_ntc = 0.0;
    
    r_ntc = 10.0f*vol/(ADC_REF-vol);
    
    for(uint8_t i=0;i<160;i++)
    {
        if(r_ntc >= ntc_temperature[i][0])
        {
            tmp = (unsigned char) ntc_temperature[i][1] + 40;
            break;
        }
    }

    return tmp;
}

static uint8_t ChipTemplateConvert(uint16_t vol)
{
    uint8_t tmp = 0;
    
    //tmp = (float)(1450 - vol) / 4.1f + 25 + 40;
    tmp = (float)(1430 - vol) / 4.3f + 25 + 40;

    return tmp;
}
static uint16_t BoardPowerCurConvert(uint16_t arg)
{
    uint16_t cur = 0;
    cur = arg/50*24;
    return cur;
}

static uint16_t Hall5VConvert(uint16_t arg)
{
    uint16_t vol = 0;
    vol = arg*2;
    return vol;
}

static void RefreashTemplate(void)
{
    app_main_can.mainTxMes1.mes.tmpChip = ChipTemplateConvert(dv_ADC.fread(AN_Chip_TMP_Chl));
    app_main_can.mainTxMes1.mes.Vrefin  = dv_ADC.fread(AN_Ref_Vol_Chl);
    app_main_can.mainTxMes1.mes.power = BoardPowerCurConvert(dv_ADC.fread(AN_Power_Chl));
    app_main_can.mainTxMes1.mes.hallvol  = Hall5VConvert(dv_ADC.fread(AN_HALLVOL_CH));
}

void app_anlog_run(void)
{
    RefreashTemplate();
}
